//	NAME:			McImagePolong.cpp
//
//	SYNOPSIS:		McPolong.h
//
//	DESCRIPTION:	This file define all functions for class McImagePolong
//

//	FILES:			McImagePolong.h
//
//	AUTHORS:		Zhuowen Tu

#include "mcPointImage.h"
#include "math.h"

McImagePoint::McImagePoint()
{
	x = 0;
	y = 0;
}

McImagePoint::McImagePoint(const int x1, const int y1)
{
	x = x1;
	y = y1;
}

// OPERATORS
McImagePoint McImagePoint::operator+(const McImagePoint& p) const
{
	McImagePoint newpt(x+p.X(), y+p.Y());
	return newpt;
}

McImagePoint McImagePoint::operator-(const McImagePoint& p) const
{
	McImagePoint newpt(x-p.X(), y-p.Y());
	return newpt;
}

McImagePoint& McImagePoint::operator=(const McImagePoint& p)
{
	if (this != &p)
	{
		x = p.X();
		y = p.Y();
	}
	return *this;
}

bool McImagePoint::operator==(const McImagePoint& p) const
{
	if (x==p.X() && y==p.Y())
		return true;
	else
		return false;
}

ofstream & McImagePoint::operator>>(ofstream &strm)
{
	strm.write((char*)&x, sizeof(x));
	strm.write((char*)&y, sizeof(y));

	return strm;
}

ifstream & McImagePoint::operator<<(ifstream &strm)
{
	strm.read((char*)&x, sizeof(x));
	strm.read((char*)&y, sizeof(y));

	return strm;
}

//ofstream & McImagePoint::operator>>(ofstream &strm, McImagePoint &im)
//{
//	strm.write((char*)&(im.x), sizeof(im.x));
//	strm.write((char*)&(im.y), sizeof(im.y));
//
//	return strm;
//
//}
//
//ifstream & McImagePoint::operator << (ifstream &strm, McImagePoint &im)
//{
//	strm.read((char*)&(im.x), sizeof(im.x));
//	strm.read((char*)&(im.y), sizeof(im.y));
//
//	return strm;
//
//}

void McImagePoint::Set(const McPoint2D pt)
{
	int temp1,temp2;

	temp1 = (long)ceil(pt.X());
	temp2 = (long)floor(pt.X());
	if (fabs(pt.X()-temp1) < fabs(pt.X()-temp2))
		x = temp1;
	else
		x = temp2;

	temp1 = (long)ceil(pt.Y());
	temp2 = (long)floor(pt.Y());
	if (fabs(pt.Y()-temp1) < fabs(pt.Y()-temp2))
		y = temp1;
	else
		y = temp2;
}
